package com.smasher.dialog.progress

import android.content.res.ColorStateList
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.smasher.dialog.BaseDialogFragment
import com.smasher.dialog.R
import com.smasher.dialog.databinding.FragmentDialogProgressBinding
import kotlinx.coroutines.launch

/**
 * @author HXZ_Android
 */
class ProgressDialogFragment : BaseDialogFragment() {

    private lateinit var mBinding: FragmentDialogProgressBinding

    private var progressColorDefault = R.color.color_CBF3D4
    private var textColor = R.color.color_CBF3D4
    private var backgroundDefault = R.drawable.dialog_background
    private var charSequence: CharSequence? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //R.style.CustomDialog
        setStyle(STYLE_NO_TITLE, 0)
        //获取参数
        arguments?.let {
            charSequence = it.getCharSequence(ARG_MESSAGE)
            progressColorDefault = it.getInt(ARG_PROGRESS_COLOR)
            textColor = it.getInt(ARG_TEXT_COLOR)
            backgroundDefault = it.getInt(ARG_BACKGROUND)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentDialogProgressBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun initView() {
        updateText()
    }

    private fun updateText() {
        lifecycleScope.launch {
            mBinding.progressText.text = charSequence
            mBinding.progressText.setTextColor(ContextCompat.getColor(requireContext(), textColor))
            mBinding.progressBar.indeterminateTintList = buildColorStateList(progressColorDefault)
        }
    }

    override fun initData() {

    }

    override fun onStart() {
        super.onStart()
        if (dialog != null) {
            dialog!!.setCanceledOnTouchOutside(false)
            // 设置宽度为屏宽、靠近屏幕底部。
            val window = dialog!!.window
            if (window != null) {
                window.setBackgroundDrawableResource(backgroundDefault)
                window.decorView.setPadding(0, 0, 0, 0)
                val wlp = window.attributes
                wlp.gravity = Gravity.CENTER
                wlp.width = WindowManager.LayoutParams.WRAP_CONTENT
                wlp.height = WindowManager.LayoutParams.WRAP_CONTENT
                window.attributes = wlp
            }
        }
    }

    private fun buildColorStateList(color: Int): ColorStateList {
        val colors = intArrayOf(color)
        val states = arrayOfNulls<IntArray>(1)
        states[0] = intArrayOf()
        return ColorStateList(states, colors)
    }

    fun setTextOnShown(charSequence: CharSequence) {
        if (isAdded && !isDetached && !isRemoving) {
            lifecycleScope.launch {
                mBinding.progressText.text = charSequence
            }
        }
    }

    companion object {

        private const val TAG = "PROGRESS"

        private const val ARG_MESSAGE = "arg_message"
        private const val ARG_PROGRESS_COLOR = "arg_progress_color"
        private const val ARG_TEXT_COLOR = "arg_text_color"
        private const val ARG_BACKGROUND = "arg_background"

        @JvmStatic
        fun newInstance(option: ProgressOption): ProgressDialogFragment {
            return ProgressDialogFragment().apply {
                arguments = Bundle().apply {
                    putCharSequence(ARG_MESSAGE, option.message)
                    putInt(ARG_PROGRESS_COLOR, option.progressColorRes)
                    putInt(ARG_TEXT_COLOR, option.textColorRes)
                    putInt(ARG_BACKGROUND, option.backgroundRes)
                }
            }
        }
    }
}